package org.example.conrtoller;

import org.example.Imp.TeacherServiceImp;
import org.example.domain.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class LoginInfoController {
    @Autowired
    private TeacherServiceImp teacherServiceImp;
    @RequestMapping("/common/exit")
    public String exit(HttpSession httpSession){
//        Teacher teacher = (Teacher)httpSession.getAttribute("loginTeacher");
//        if (teacher==null){}//表示没有登录然后就访问这个页面 然后退出  等下要做权限访问 所以这里不用管 访问到这个页面都是存在信息的
        httpSession.removeAttribute("loginTeacher");
        return "t_login_page";
    }
    @RequestMapping("/common/updatePwd")
    public String updatePwd(){
        return "common/updatePassword";
    }
    @ResponseBody
    @RequestMapping("/common/commitUpdatePwd")
    public String commitUpdatePwd(String oldPwd,String newPwd,HttpSession session){
        Teacher teacher = (Teacher) session.getAttribute("loginTeacher");
        //因为做了登录认证 所以 这里能访问说明是登录进来的 session中肯定有数据 不用验证
        Boolean result = teacherServiceImp.surePassword(teacher.getPass(),oldPwd);
        if (!result){
            //表示不相等
            return "1";
        }
        Map<String,String> map = new HashMap<>();
        map.put("mnemonic_code",teacher.getMnemonic_code());
        map.put("newPwd",newPwd);
        teacherServiceImp.commitUpdatePwd(map);
        return "0";
    }

}
